package olegroshka.src.com.bofa.olegroshka.format;

import java.util.Locale;


public class TemplateFormatter implements RecordFormatter {

	private static final int[] EMPTY_ARRAY = new int[]{};
	private final Locale locale;
	private final String template;
	private final int[] columns;
	
	public TemplateFormatter(String template, int... columns) {
		this.template = template;
		this.columns = columns;
		this.locale = null;
	}
	
	public TemplateFormatter(String template) {
		this(template, EMPTY_ARRAY);
	}
	
	public TemplateFormatter(Locale locale, String template, int[] columns) {
		this.template = template;
		this.columns = columns;
		this.locale = locale;
	}

	public String decorate(Object[] in) {
		Object[] args = new Object[columns.length];
		for (int i = 0; i < columns.length; i++) {
			args[i] = in[columns[i]];
		}
		return locale != null ? 
				String.format(locale, template, args) : 
					String.format(template, args);
	}


}
